package com.gavin.sample.sms;

import java.util.Date;

import org.json.JSONObject;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

public class SmsUtil {
	public static final String URL_SINGLE_SMS = "https://sms-api.luosimao.com/v1/send.json";
	public static final String SMS_CONTENT = "尊敬的用户，您的资金账户（中信银行提供）已开通成功。请登录[我的链化]—[资金账户]查看详情，点击[提现]操作，完成资金账户激活。【链化网】";
	public static final String SMS_CONTENT2 = "尊敬的用户，您的资金账户（中信银行提供）已成功激活。最真实的交易，最畅通的渠道，只在链化网。【链化网】";
	public static final String SMS_CONTENT3 = "您的链付通账户于2015-05-20 13:30:45提现100元，可用余额500元，请登陆平台查看详情【链化网】";
	
	public static boolean sendSms(String mobile, String message) {
		System.out.println(new Date().toLocaleString());
		try {
			HttpResponse<JsonNode> jsonResponse = Unirest.post(URL_SINGLE_SMS).basicAuth("api", "key-550f70f2709415f605501beb7e6ff193")
				.field("mobile", mobile)
				.field("message", message)
				.asJson();
			JSONObject jsonObj = jsonResponse.getBody().getObject();
			int error_code = jsonObj.getInt("error");
			String error_msg = jsonObj.getString("msg");
			if(error_code==0){
                System.out.println("Send message success.");
            }else{
                System.out.println("Send message failed,code is "+error_code+",msg is "+error_msg);
            }
		} catch (UnirestException e) {
			return false;
		}
		
		return false;
	}
	
	public static void main(String[] args) throws InterruptedException {
//		System.out.println(SMS_CONTENT.length());
		for (int i = 0; i < 1; i++) {
			SmsUtil.sendSms("18616154199", SMS_CONTENT3);
			Thread.sleep(1000l);
		}
	}
}
